package com.icode.gulimall.product.service.impl;

import com.icode.gulimall.product.config.MyThreadPoolConfig;
import com.icode.gulimall.product.entity.SkuImagesEntity;
import com.icode.gulimall.product.entity.SkuSaleAttrValueEntity;
import com.icode.gulimall.product.entity.SpuInfoDescEntity;
import com.icode.gulimall.product.service.*;
import com.icode.gulimall.product.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.icode.common.utils.PageUtils;
import com.icode.common.utils.Query;

import com.icode.gulimall.product.dao.SkuInfoDao;
import com.icode.gulimall.product.entity.SkuInfoEntity;
import org.springframework.util.StringUtils;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    SkuImagesService skuImagesService;

    @Autowired
    SpuInfoDescService spuInfoDescService;

    @Autowired
    AttrGroupService attrGroupService;

    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    ThreadPoolExecutor executor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);

    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfo) {
        this.baseMapper.insert(skuInfo);
    }

    @Override
    public PageUtils querySkuInfoPage(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and(wapper -> wapper.eq("sku_id", key).or().eq("sku_name", key));
        }
        String catalogId = (String) params.get("catelogId");
        if (!StringUtils.isEmpty(catalogId) && !"0".equalsIgnoreCase(catalogId)) {
            queryWrapper.eq("catalog_id", catalogId);
        }
        String brandId = (String) params.get("brandId");
        if (!StringUtils.isEmpty(brandId) && !"0".equalsIgnoreCase(brandId)) {
            queryWrapper.eq("brand_id", brandId);
        }
        String min = (String) params.get("min");
        if (!StringUtils.isEmpty(min)) {
            queryWrapper.ge("price", min);
        }
        String max = (String) params.get("max");
        if (!StringUtils.isEmpty(max)) {
            BigDecimal bigDecimal = new BigDecimal(max);
            if (bigDecimal.compareTo(new BigDecimal("0")) == 1) {
                queryWrapper.le("price", max);
            }
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public SkuInfoEntity getSkuEntityById(Long skuId) {
        return this.getOne(new QueryWrapper<SkuInfoEntity>().eq("sku_id", skuId));
    }

    @Override
    public List<SkuInfoEntity> getSkuBySpuId(Long spuId) {
        List<SkuInfoEntity> list = this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
        return list;
    }

    @Override
    public SkuItemVo item(Long skuId){
        SkuItemVo skuItemVo = new SkuItemVo();
        //sku基本信息   sku_info
        CompletableFuture<SkuInfoEntity> skuInfoTask = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity info = this.getById(skuId);
            skuItemVo.setInfo(info);
            return info;
        }, executor);

        CompletableFuture<Void> skuImagesTask = CompletableFuture.runAsync(() -> {
            //获取SkuImages
            List<SkuImagesEntity> bySkuId = skuImagesService.getBySkuId(skuId);
            skuItemVo.setImages(bySkuId);
        }, executor);

        CompletableFuture<Void> saleAttrTask = skuInfoTask.thenAcceptAsync(res -> {
            //获取spu的销售属性组合
            Long spuId = res.getSpuId();
            List<SkuItemSaleAttrVo> skuSaleAttrBySkuIds = skuSaleAttrValueService.getSkuSaleAttrBySpuId(spuId);
            skuItemVo.setSaleAttr(skuSaleAttrBySkuIds);
        }, executor);

        CompletableFuture<Void> spuDescTask = skuInfoTask.thenAcceptAsync(res -> {
            //获取spu的介绍
            Long spuId = res.getSpuId();
            SpuInfoDescEntity byId = spuInfoDescService.getById(spuId);
            skuItemVo.setDesc(byId);
        }, executor);

        CompletableFuture<Void> productAttrTask = skuInfoTask.thenAcceptAsync(res -> {
            //获取spu的规格参数信息
            Long spuId = res.getSpuId();
            Long catalogId = res.getCatalogId();
            List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
            skuItemVo.setGroupAttrs(attrGroupVos);
        }, executor);

        try {
            CompletableFuture.allOf(skuImagesTask,saleAttrTask,
                    productAttrTask, spuDescTask).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


        return skuItemVo;
    }

    @Override
    public Map<Long, BigDecimal> getSkuPriceBySkuIds(List<Long> skuIds) {
        List<SkuInfoEntity> skuInfoEntities = this.listByIds(skuIds);
        Map<Long, BigDecimal> collect = skuInfoEntities.stream().collect(Collectors.toMap(SkuInfoEntity::getSkuId, SkuInfoEntity::getPrice));
        return collect;
    }
}